package com.heima.reggie.exception;

import com.heima.reggie.common.R;
import com.heima.reggie.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
//@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
        exception.printStackTrace();
        //log.error(exception.getMessage());
        System.out.println(exception.getMessage());
       /* if (exception.getMessage().contains("#23000")) {
            String[] split = exception.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }*/
        return R.error("已存在");
    }
    //处理自定义异常
    @ExceptionHandler(CustomException.class)
    public R handlerCustException(CustomException e) {
        e.printStackTrace();
        return R.error(e.getMessage());
    }
}
